package com.yt.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/*
 * 自定义工具类,wangchenyu
 * */
@Slf4j
public class CustomizeUtil {

    // 时间格式化
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * @Description: 判断list为空
     * @Author: chenyu.wang
     * @Params: [list]
     * @Date: 2020.11.05 16:07
     */
    public static boolean listIsNull(List<?> list) {
        return null == list || list.isEmpty();
    }

    /**
     * @Description: 判断String为空
     * @Author: chenyu.wang
     * @Params: [s]
     * @Date: 2020.11.05 16:10
     */
    public static boolean strIsNull(String s) {
        return null == s || s.trim().isEmpty();
    }

    /**
     * @Description: 判断list不为空
     * @Author: chenyu.wang
     * @Params: [list]
     * @Date: 2020.11.05 16:44
     */
    public static boolean listIsNotNull(List<?> list) {
        return null != list && !list.isEmpty();
    }

    /**
     * @Description: 判断String不为空
     * @Author: chenyu.wang
     * @Params: [s]
     * @Date: 2020.11.05 16:45
     */
    public static boolean strIsNotNull(String s) {
        return null != s && !s.trim().isEmpty();
    }

    /**
     * @return : java.util.Date
     * @Author :chenyu.wang
     * @Description : 获取当天开始时间
     * @Date : 11:23 2021.01.12
     * @Param : []
     **/
    public static String getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(new Date());
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return SDF.format(todayStart.getTime());
    }

    /**
     * @return : java.util.Date
     * @Author :chenyu.wang
     * @Description : 获取当天结束时间
     * @Date : 11:24 2021.01.12
     * @Param : []
     **/
    public static String getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(new Date());
        todayEnd.set(Calendar.HOUR_OF_DAY, 0);
        todayEnd.set(Calendar.MINUTE, 0);
        todayEnd.set(Calendar.SECOND, 0);
        todayEnd.set(Calendar.MILLISECOND, 0);
        todayEnd.add(Calendar.DAY_OF_MONTH, 1);
        todayEnd.add(Calendar.MILLISECOND, -1);
        return SDF.format(todayEnd.getTime());
    }

    /**
     * @return : java.lang.String
     * @Author :chenyu.wang
     * @Description : 格式化分钟为几天几小时几分钟
     * @Date : 17:13 2021.01.12
     * @Param : [minute]
     **/
    public static String getOtherDateFormat(long minute) {
        try {
            return minute / 24 / 60 + "天" + minute / 60 % 24 + "小时" + minute % 60 + "分钟";
        } catch (Exception e) {
            log.info("格式化在线时长时间失败");
            e.printStackTrace();
        }
        return "0天0小时0分钟";
    }

    /**
     * @return : int
     * @Author :chenyu.wang
     * @Description : 获取时间间隔天数
     * @Date : 11:26 2021.01.13
     * @Param : [startTime, endTime]
     **/
    public static int getTimeDay(String startTime, String endTime) {
        try {
            Date startDate = SDF.parse(startTime);
            Date endDate = SDF.parse(endTime);
            return (int) ((startDate.getTime() - endDate.getTime()) / (1000 * 3600 * 24));
        } catch (ParseException e) {
            log.error("获取时间间隔天数失败");
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @return : java.lang.String
     * @Author :chenyu.wang
     * @Description : 获取百分比
     * @Date : 11:40 2021.01.13
     * @Param : [a, b]
     **/
    public static String ADivideBPercent(BigDecimal a, BigDecimal b) {
        return b == null ? "-" :
                b.compareTo(new BigDecimal(0)) == 0 ? "-" :
                        a == null ? "0.00%" :
                                a.multiply(new BigDecimal(100)).divide(b, 2, BigDecimal.ROUND_HALF_UP) + "%";

    }

    /**
     * @return : boolean
     * @Author :chenyu.wang
     * @Description : 判断两个时间是否在分钟内
     * @Date : 16:10 2021.01.15
     * @Param : [startTime, endTime, minute]
     **/
    public static boolean checkTwoTime(Date startTime, Date endTime, Integer minute) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(startTime);
        c1.add(Calendar.MINUTE, minute);
        Date time = c1.getTime();
        if (endTime.before(time) || endTime.equals(time)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return : java.util.Date
     * @Author :chenyu.wang
     * @Description : 获取一年后的时间
     * @Date : 16:37 2021.01.20
     * @Param : [date]
     **/
    public static Date getOneYearTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        return calendar.getTime();
    }

    /**
     * @return : java.lang.String
     * @Author :chenyu.wang
     * @Description : 根据时间戳生成随机订单号
     * @Date : 2021.02.26 11:48
     * @Param : []
     **/
    public static String getOrderNo() {
        // 时间格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        // 定义随机数
        Random random = new Random();
        // 随机数长度
        int nextInt = random.nextInt(10000);
        Date date = new Date();
        String dateString = dateFormat.format(date);
        // 生成订单号
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder orderNo = stringBuilder.append(dateString).append(nextInt);
        return orderNo.toString();
    }
}
